{extend name="../../base/view/common/base" /}
{block name="style"}
<style>
    .layui-table-view .layui-table thead th {
        background-color: #f2f2f2;
    }
    .progress-time {
        color: #999;
        font-size: 12px;
    }
</style>
{/block}
<!-- 主体 -->
{block name="body"}
<div class="p-page">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="layui-form toolbar" id="searchForm">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <input type="text" name="case_number" placeholder="输入案件编号关键词" class="layui-input">
                            </div>
                            <div class="layui-inline">
                                <select name="event_type" lay-search lay-filter="eventType">
                                    <option value="">所有事件类型</option>
                                    <option value="执行启动">执行启动</option>
                                    <option value="庭审通知">庭审通知</option>
                                    <option value="调解程序">调解程序</option>
                                    <option value="材料收集">材料收集</option>
                                    <option value="缴费完成">缴费完成</option>
                                </select>
                            </div>
                            <div class="layui-inline">
                                <select name="operator" lay-search lay-filter="operator">
                                    <option value="">所有操作人</option>
                                    <!-- 动态加载操作人列表 -->
                                </select>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="searchForm">
                                    <i class="layui-icon">&#xe615;</i>搜索
                                </button>
                                <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" id="resetBtn">
                                    <i class="layui-icon">&#xe669;</i>清空
                                </button>
                            </div>
                        </div>
                    </form>
                    <table id="progressTable" lay-filter="progressTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}

<!-- 脚本 -->
{block name="script"}
<!-- <script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm tool-add" data-href="/lawcase/progress/add">+ 添加进度</button>
    </div>
</script> -->

<script type="text/html" id="column-toolbar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
</script>

<script>
const moduleInit = ['tool'];
function gouguInit() {
    var table = layui.table;
    var form = layui.form;
    var $ = layui.jquery;
    var layer = layui.layer;
    var tool = layui.tool;
    
    // 加载操作人列表
    $.get('/lawcase/api/getOperators', function(res) {
        if (res.code === 0) {
            var html = '<option value="">所有操作人</option>';
            res.data.forEach(function(item) {
                html += '<option value="' + item.id + '">' + item.name + '</option>';
            });
            $('select[name="operator"]').html(html);
            form.render('select');
        }
    });
    
    // 初始化表格
    table.render({
        elem: '#progressTable',
        url: '/lawcase/progress/index',
        toolbar: '#toolbar',
        where: {
            case_id: getUrlParam('case_id'),
            case_number: '',
            event_type: '',
            operator: ''
        },
        cols: [[
            {field: 'case_number', title: '案件编号', width: 200, templet: function(d){
                return '<a href="javascript:;" onclick="showCaseDetail(\'' + d.case_id + '\')" class="layui-table-link">' + d.case_number + '</a>';
            }},
            {field: 'party_names', title: '委托人', width: 150, templet: function(d){
                return Array.isArray(d.party_names) ? d.party_names.join(', ') : '';
            }},
            {field: 'event_type', title: '事件类型', width: 120, align: 'center'},
            {field: 'content', title: '进度内容', minWidth: 200},
            {field: 'event_time', title: '事件时间', width: 120, align: 'center'},
            {field: 'operator_name', title: '操作人', width: 120, align: 'center'},
            {field: 'create_time', title: '创建时间', width: 180, align: 'center'}
        ]],
        page: true,
        parseData: function(res){
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.data.count,  // 修复：从 data.count 获取总记录数
                "data": res.data.data
            };
        }
    });

    // 搜索表单提交
    form.on('submit(searchForm)', function(data) {
        table.reload('progressTable', {
            where: data.field,
            page: {curr: 1} // 重置为第一页
        });
        return false;
    });

    // 重置按钮
    $('#resetBtn').on('click', function() {
        // 重置表单
        $('#searchForm')[0].reset();
        // 重置下拉框
        layui.form.render('select');
        
        // 重新加载表格数据（无条件查询）
        table.reload('progressTable', {
            where: {
                case_id: getUrlParam('case_id'),
                case_number: '',
                event_type: '',
                operator: ''
            },
            page: {curr: 1} // 重置到第一页
        });
    });

    // 工具栏事件
    table.on('toolbar(progressTable)', function(obj) {
        if (obj.event === 'tool-add') {
            tool.side("/lawcase/progress/add", '添加进度');
        }
    });

    // 行工具事件
    table.on('tool(progressTable)', function(obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            tool.side("/lawcase/progress/edit?id=" + data.id, '编辑进度');
        } else if (obj.event === 'delete') {
            layer.confirm('确定删除该进度记录吗？', function(index) {
                $.post('/lawcase/progress/delete', {id: data.id}, function(res) {
                    if (res.code === 0) {
                        layer.msg(res.msg, {icon: 1});
                        table.reload('progressTable');
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                });
                layer.close(index);
            });
        }
    });
}

// 添加URL参数获取函数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}
// 添加在gouguInit函数内
function showCaseDetail(caseId) {
    layui.tool.side('/lawcase/lawcase/view?id=' + caseId, '案件详情');
}
</script>
{/block}